package com.example.xunua_zhinengjiaotong.Dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;


public class CarSpeedDialog extends AlertDialog{
    private MyApp myApp;
    private Context context;
    private Button btn_feilv;
    private DataListener listener;
    private Spinner spinner;
    private HttpUtil util=new HttpUtil();
    private TextView tv_speed;


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                tv_speed.setText(""+msg.obj);
            } else if (msg.what == -1) {
//                10秒后未查询到则执行此操作
                tv_speed.setText("查询失败！");
            }
        }
    };

    public CarSpeedDialog(Context context, DataListener listener, MyApp myApp) {
        super(context,R.style.myDialog);
        this.listener=listener;
        this.context=context;
        this.myApp=myApp;
    }

    protected CarSpeedDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected CarSpeedDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_dialog_car_speed);
        initView();
    }

    private void initView() {
            tv_speed=findViewById(R.id.trolley_dialog_tv02);
            spinner = findViewById(R.id.trolley_dialog_spinner);
            spinner.setAdapter(new ArrayAdapter<>(context, R.layout.support_simple_spinner_dropdown_item,
                    new String[]{"一号小车", "二号小车", "三号小车", "四号小车"}));
            btn_feilv = findViewById(R.id.trolley_dialog_btn_chaxun);
            btn_feilv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    String str = spinner.getSelectedItem().toString();
//                    listener.sendData(str);
//                    dismiss();
                    int id= (int) (spinner.getSelectedItemId()+1);
                    showCarSpeed(id+"");
                }
            });
    }
    private void showCarSpeed(final String id) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject getCarSpeed = util.connectServer("{\"CarId\":" + id + "}", myApp, "GetCarSpeed");
                    String carSpeed = getCarSpeed.getString("CarSpeed");
                    Message message=new Message();
                    message.what=0;
                    message.obj=carSpeed;
                    handler.sendMessage(message);
                }catch (Exception e){
                    handler.sendEmptyMessage(-1);
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public interface DataListener {
        void sendData(String s);
    }
}
